﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.DataAccess
{
    public class AnswerDAO
    {
        private TestOnlineDBEntities db = new TestOnlineDBEntities();

        public int Insert(Answer answer)
        {
            db.Answers.AddObject(answer);
            int result = db.SaveChanges();
            return result;
        }

        public List<Answer> SelectByQuestionId(int id)
        {
            var answers = db.Answers.Where(a => a.QuestionId == id).ToList();
            return answers;
        }
        public List<Question> Insert(List<Question> questions, int createrId)
        {
            try
            {
                for (int i = 0; i < questions.Count; i++)
                {
                    questions[i].UserId = createrId;
                    this.db.Questions.AddObject(questions[i]);
                }
                this.db.SaveChanges();
                return questions;
            }
            catch (Exception)
            {
                return null;
            }
            
        }

        /// <summary>
        /// Select Answer by AnswerId
        /// </summary>
        /// <param name="answerId">Id of Answer</param>
        /// <returns>Answer object</returns>

        public Answer SelectByAnswerId(int answerId)
        {
            var answer = db.Answers.FirstOrDefault(x => x.AnswerId == answerId);
            return answer;
        }
    }
}